package com.rt.cms.service.impl;

import com.rt.cms.common.base.Page;
import com.rt.cms.common.base.PageResultSet;
import com.rt.cms.dao.ArchivesMapper;
import com.rt.cms.dao.TbApprovalRecordMapper;
import com.rt.cms.dao.TbCompanyInfoMapper;
import com.rt.cms.data.dto.company.ArchivesDto;
import com.rt.cms.data.dto.company.GuaranteeDto;
import com.rt.cms.data.entity.TbApprovalRecord;
import com.rt.cms.data.entity.TbCompanyInfo;
import com.rt.cms.data.entity.TbCompayUploadInfo;
import com.rt.cms.data.entity.system.BizException;
import com.rt.cms.data.requestParam.CompanyParam;
import com.rt.cms.service.ArchivesService;
import com.rt.cms.service.CompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 企业信息
 * Created by lgx on 2018/05/24.
 */
@Service
public class ArchivesServiceImpl implements ArchivesService {

    @Autowired
    private ArchivesMapper archivesMapper;

    @Autowired
    private TbApprovalRecordMapper approvalRecordMapper;


    @Override
    public int create(TbCompanyInfo tbCompayInfo) throws BizException {

        return archivesMapper.create(tbCompayInfo);
    }

    @Override
    public int update(TbCompanyInfo tbCompanyInfo) {
        return archivesMapper.update(tbCompanyInfo);
    }

    @Override
    public PageResultSet<ArchivesDto> findByPage(Page page) {

        CompanyParam companyParam = new CompanyParam();
        companyParam.setLimit(page.getLimit());
        companyParam.setOffset(page.getOffset());
        List<ArchivesDto> dtoList = new ArrayList<>();
        archivesMapper.selectByParam(companyParam).forEach(company -> {
            ArchivesDto archivesDto = new ArchivesDto(company);
            dtoList.add(archivesDto);
        });
        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(dtoList.size());
        return resultSet;
    }

    @Override
    public void delete(Integer id) {
        archivesMapper.delete(id);
    }

    @Override
    public TbCompanyInfo findById(int id) {
        return archivesMapper.findById(id);
    }

    @Override
    public PageResultSet<ArchivesDto> findByStatus(Integer status, Page page, String ids, String comName, Integer checkId, String dormant,String selectType) {

        CompanyParam companyParam = new CompanyParam();
        companyParam.setLimit(page.getLimit());
        companyParam.setOffset(page.getOffset());
        companyParam.setStatus(status);
        companyParam.setDormant(dormant);
        companyParam.setIds(ids);
        companyParam.setCheckId(checkId);
        companyParam.setComName(comName);
        companyParam.setSelectType(selectType);
        Long count = archivesMapper.selectCount(companyParam);
        List<ArchivesDto> dtoList = new ArrayList<>();
        archivesMapper.selectByParam(companyParam).forEach(company -> {
            List<TbApprovalRecord> list = approvalRecordMapper.queryApprovalRecordByUsrId(company.getId());
            int msgCount = 0;
            int dormanyCount = 0;
            int waitDormanyCount = 0;
            if (list != null && list.size() > 0) {
                msgCount = list.size();
            }

            List<TbApprovalRecord> dormanyList = approvalRecordMapper.queryDormanyRecordByUsrId(company.getId());
            if (dormanyList != null && dormanyList.size() > 0) {
                dormanyCount = dormanyList.size();
            }

            List<TbApprovalRecord> waitDormanyList = approvalRecordMapper.queryWaitDormanyRecordByUsrId(company.getId());
            if (waitDormanyList != null && waitDormanyList.size() > 0) {
                waitDormanyCount = waitDormanyList.size();
            }

            ArchivesDto archivesDto = new ArchivesDto(company);
            archivesDto.setMsgCount(msgCount);
            archivesDto.setDormanyCount(dormanyCount);
            archivesDto.setWaitDormanyCount(waitDormanyCount);
            dtoList.add(archivesDto);
        });
        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(count);
        return resultSet;
    }


    @Override
    public PageResultSet<ArchivesDto> findByDormantStatus(String dormant, Page page, String ids) {

        CompanyParam companyParam = new CompanyParam();
        companyParam.setLimit(page.getLimit());
        companyParam.setOffset(page.getOffset());
        companyParam.setDormant(dormant);
        companyParam.setIds(ids);
        Long count = archivesMapper.selectCount(companyParam);
        List<ArchivesDto> dtoList = new ArrayList<>();
        archivesMapper.selectByParam(companyParam).forEach(company -> {
            ArchivesDto archivesDto = new ArchivesDto(company);
            dtoList.add(archivesDto);
        });
        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(count);
        return resultSet;
    }

    /**
     * 企业详细信息
     *
     * @param
     * @return
     * @authon Wangzs
     */
    @Override
    public ArchivesDto findCompanyDetailById(int id) {

        TbCompanyInfo tbCompanyInfo = archivesMapper.findCompanyDetailById(id);
        ArchivesDto archivesDto = new ArchivesDto(tbCompanyInfo);

        archivesDto.setPicURL(tbCompanyInfo.getPicURL());
        archivesDto.setPicName(tbCompanyInfo.getPicName());
        archivesDto.setIsDisplay(tbCompanyInfo.getIsDisplay());
        archivesDto.setUploadId(tbCompanyInfo.getId());
        archivesDto.setArchivesDtoList(findGuaranteeDetailById(id));
        return archivesDto;

    }


    /**
     * 担保企业详细信息
     *
     * @param
     * @return
     * @authon Wangzs
     */
    @Override
    public List<ArchivesDto> findGuaranteeDetailById(Integer id) {
        List<ArchivesDto> archivesDtoList = new ArrayList<>();
        archivesMapper.findGuaranteeDetailById(id).forEach(tbCompanyInfo -> {
            //GuaranteeDto guaranteeDto = new GuaranteeDto(tbCompanyInfo.getId(),tbCompanyInfo.getPicName(),
            //       tbCompanyInfo.getPicURL(),tbCompanyInfo.getIsDisplay());
            ArchivesDto archivesDto = new ArchivesDto(tbCompanyInfo);
            archivesDto.setPicURL(tbCompanyInfo.getPicURL());
            archivesDto.setPicName(tbCompanyInfo.getPicName());
            archivesDto.setIsDisplay(tbCompanyInfo.getIsDisplay());
            archivesDto.setUploadId(tbCompanyInfo.getUploadId());

            //archivesDto.getGuaranteeDtoList().add(guaranteeDto);
            archivesDtoList.add(archivesDto);
        });
        return archivesDtoList;
    }

    @Override
    public PageResultSet<ArchivesDto> findByOperateID(Page page, Integer id) {
        CompanyParam companyParam = new CompanyParam();
        companyParam.setLimit(page.getLimit());
        companyParam.setOffset(page.getOffset());
        companyParam.setId(id);
        List<ArchivesDto> dtoList = new ArrayList<>();
        Long count = archivesMapper.selectCount(companyParam);
        archivesMapper.selectByParam(companyParam).forEach(company -> {
            ArchivesDto archivesDto = new ArchivesDto(company);
            dtoList.add(archivesDto);
        });

        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(count);
        return resultSet;
    }


    @Override
    public PageResultSet<ArchivesDto> selectByUseParam(String status, Page page, String ids, String comName, Integer checkId, String dormant, String comType, String inputTime) {

        CompanyParam companyParam = new CompanyParam();
        companyParam.setLimit(page.getLimit());
        companyParam.setOffset(page.getOffset());
        companyParam.setStatus("".equals(status) ? null : Integer.parseInt(status));
        companyParam.setDormant(dormant);
        companyParam.setIds(ids);
        companyParam.setCheckId(checkId);
        companyParam.setComName(comName);
        companyParam.setComType("".equals(comType) ? null : Integer.parseInt(comType));
        companyParam.setInputTime(inputTime);

        Long count = archivesMapper.selectUseCount(companyParam);
        List<ArchivesDto> dtoList = new ArrayList<>();
        archivesMapper.selectByUseParam(companyParam).forEach(company -> {
            List<TbApprovalRecord> list = approvalRecordMapper.queryApprovalRecordByUsrId(company.getId());
            int msgCount = 0;
            int dormanyCount = 0;
            if (list != null && list.size() > 0) {
                msgCount = list.size();
            }

            List<TbApprovalRecord> dormanyList = approvalRecordMapper.queryDormanyRecordByUsrId(company.getId());
            if (dormanyList != null && dormanyList.size() > 0) {
                dormanyCount = dormanyList.size();
            }

            ArchivesDto archivesDto = new ArchivesDto(company);
            archivesDto.setMsgCount(msgCount);
            archivesDto.setDormanyCount(dormanyCount);
            dtoList.add(archivesDto);
        });
        PageResultSet<ArchivesDto> resultSet = new PageResultSet<>();
        resultSet.setRows(dtoList);
        resultSet.setTotal(count);
        return resultSet;
    }
}
